const connectiondb = require("../sql/mysql")

/* 
    TODO 获取所有的图书信息
*/
async function queryAll(req, res) {
   
    const sql = "select * from class_info"
    const params = []
    const result = await connectiondb(sql, params);
    // 讲数据交给前端
    res.send({ code: 200, msg: "获取学生信息成功", data: result })
}


// 删除
async function del(req,res){
    const ccid = req.body;
    const cid1=ccid.cid
    const sql = "delete from  class_info where cid =? " 
    await connectiondb(sql,cid1)
    res.send({code:200,msg:'删除成功'})
}


/* 
    TODO 添加学生信息
*/
async function add(req,res){
    const {cname} = req.body;
    const sql = "insert into class_info value(null,?)";
    await connectiondb(sql,[cname])
    res.send({code:200,msg:'添加学生信息成功'})
}



/* 
    TODO 修改图书信息
*/
async function update(req, res) {
    const {aid,cname}  = req.body;
     console.log(aid,cname);
    // console.log(cid2)
    const sql = "update class_info set cname = ?   where cid = ?"
    await connectiondb(sql,[cname,aid])
    res.send({code:200,msg:"修改图书信息成功",})
}


/* 
    TODO 获取图书ID

*/
async function queryOne(req,res){
    const {cid} = req.body;
    const sql = "select * from class_info where cid = ? "
    const result = await connectiondb(sql,[cid])
    res.send({code:200,msg:'根据图书获取ID成功',data:result[0]})
}



/* 
    TODO 根据图书名进行模糊查询
*/
async function likename(req,res){
    const {cname} = req.body;
    const sql = `select * from class_info where cname like '%${cname}%'`;
    const result = await connectiondb(sql,[])
    res.send({code:200,msg:'模糊查询成功',data:result})
}



// 暴露
module.exports ={
    queryAll,
    del,
    add,
    update,
    queryOne,
    likename
}